package com.lmbx.shzh.service.impl;

import com.google.gson.Gson;
import com.lmbx.shzh.domain.common.ResponseResult;
import com.lmbx.shzh.domain.vo.AlarmList;
import com.lmbx.shzh.domain.vo.AlarmListVO;
import com.lmbx.shzh.mapper.RainMapper;
import com.lmbx.shzh.service.RainService;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.util.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class RainServiceImpl implements RainService {

    @Autowired
    private RainMapper mapper;

    /**
     * 获取预报雨量
     *
     * @return
     */
    @Override
    public ResponseResult getRain() {
        List<Map<String, String>> map = mapper.getRain();
        return ResponseResult.success(map);
    }

    /**
     * 实时雨情统计-
     *
     * @return
     */
    @Override
    public ResponseResult getRainNum() {
        List<Map<String, String>> map = mapper.getRainNum();
        return ResponseResult.success(map);
    }

    /**
     * 获取雨量超警
     *
     * @param vCode
     * @param hour
     * @return
     */
    @Override
    public ResponseResult getRainTown(String vCode, int hour) {
        // 省2位 = 2 市4位 = 3  县6位 = 4
        int vCodeLevel = getCodeLevel(vCode);
        int vCodeLength = vCode.trim().length();
        List<Map<String, String>> map = mapper.getRainTown(vCode, hour, vCodeLevel, vCodeLength);
        return ResponseResult.success(map);
    }

    /**
     * 水库水情1
     *
     * @return
     */
    @Override
    public ResponseResult getRsvrAll() {
        List<Map<String, String>> map = mapper.getRsvrAll();
        return ResponseResult.success(map);
    }

    /**
     * 水库水情2
     *
     * @return
     */
    @Override
    public ResponseResult getRsvr() {
        List<Map<String, String>> map = mapper.getRsvr();
        return ResponseResult.success(map);
    }

    /**
     * 获取河道水情1
     *
     * @return
     */
    @Override
    public ResponseResult getRsverAll() {
        List<Map<String, String>> map = mapper.getRsverAll();
        return ResponseResult.success(map);
    }

    /**
     * 获取河道水情2
     *
     * @return
     */
    @Override
    public ResponseResult getRsver() {
        List<Map<String, String>> map = mapper.getRsver();
        return ResponseResult.success(map);
    }

    /**
     * 获取山洪预警
     *
     * @return
     */
    @Override
    public ResponseResult getWarnInfo() {
        List<Map<String, String>> map = mapper.getWarnInfo();
        return ResponseResult.success(map);
    }

    /**
     * 获取预报预警
     *
     * @return
     */
    @Override
    public ResponseResult getRainFull() {
        List<Map<String, String>> map = mapper.getRainFull();
        return ResponseResult.success(map);
    }

    /**
     * 获得测站实时降雨
     *
     * @param vCode
     * @return
     */
    @Override
    public ResponseResult getStationRain(String vCode, int num) {
        int vCodeLevel = getCodeLevel(vCode);
        int vCodeLength = vCode.trim().length();
        List<Map<String, String>> map = mapper.getStationRain(vCode, vCodeLevel, num, vCodeLength);
        return ResponseResult.success(map);
    }

    /**
     * 获取一小时雨量
     * @param vCode
     * @param hour
     * @return
     */
    @Override
    public ResponseResult rain(String vCode, int hour) {
        int vCodeLevel = getCodeLevel(vCode);
        int vCodeLength = vCode.trim().length();
        List<Map<String, String>> map = mapper.rain(vCode, hour, vCodeLevel, vCodeLength);
        return ResponseResult.success(map);
    }

    /**
     * 获取H小时实时数据
     * @param vCode
     * @param hour
     * @return
     */
    @Override
    public ResponseResult ycRain(String vCode, int hour) {
        int vCodeLevel = getCodeLevel(vCode);
        int vCodeLength = vCode.trim().length();
        List<Map<String, String>> map = mapper.ycRain(vCode, hour, vCodeLevel, vCodeLength);
        return ResponseResult.success(map);
    }

    @Override
    public ResponseResult getRain(String vCode, Date bt, Date et) {
        int vCodeLevel = getCodeLevel(vCode);
        int vCodeLength = vCode.trim().length();

        List<Map<String, String>> map = mapper.getRain(vCode, vCodeLevel, vCodeLength, bt, et);
        return ResponseResult.success(map);
    }

    @Override
    public ResponseResult getRiver(String vCode, Date bt, Date et) {
        int vCodeLevel = getCodeLevel(vCode);
        int vCodeLength = vCode.trim().length();

        List<Map<String, String>> map = mapper.getRiver(vCode, vCodeLevel, vCodeLength, bt, et);
        return ResponseResult.success(map);
    }

    @Override
    public ResponseResult getRsvr(String vCode, Date bt, Date et) {
        int vCodeLevel = getCodeLevel(vCode);
        int vCodeLength = vCode.trim().length();

        List<Map<String, String>> map = mapper.getRsvr(vCode, vCodeLevel, vCodeLength, bt, et);
        return ResponseResult.success(map);
    }

    @Override
    public ResponseResult getWarn(String vCode, Date bt, Date et) {
        int vCodeLevel = getCodeLevel(vCode);
        int vCodeLength = vCode.trim().length();
        List<Map<String, String>> map = mapper.warn(vCode, vCodeLevel, vCodeLength, bt, et);
        return ResponseResult.success(map);
    }

    @Override
    public ResponseResult rvfcch(String vCode, int hour) {
        int vCodeLevel = getCodeLevel(vCode);
        int vCodeLength = vCode.trim().length();
        List<Map<String, String>> map = mapper.rvfcch(vCode, hour, vCodeLevel, vCodeLength);
        return ResponseResult.success(map);
    }
    /**
     * 根据地区编码，获得对应级别码
     *
     * @param vCode
     * @return
     */
    private int getCodeLevel(String vCode) {
        int vCodeLevel = 2;
        vCode = vCode.trim();
        if (vCode.length() == 2) {
            vCodeLevel = 2;
        } else if (vCode.length() == 4) {
            vCodeLevel = 3;
        } else if (vCode.length() == 6) {
            vCodeLevel = 4;
        }
        return vCodeLevel;
    }

    @Override
    public int rainingCount(Date bt, Date et) {
        return mapper.rainingCount(bt, et);
    }

    @Override
    public int rsvrCountOverWarn(Date bt, Date et) {
        return mapper.rsvrCountOverWarn(bt, et);
    }

    @Override
    public int riverCountOverWarn(Date bt, Date et) {
        return mapper.riverCountOverWarn(bt, et);
    }

    @Override
    public Map<String, String> maxRainAddress(Date bt, Date et) {
        return mapper.maxRainAddress(bt, et);
    }

    @Override
    public int shWarnCount(Date bt, Date et, Integer gradeID, Integer statusID) {
        return mapper.shWarnCount(bt, et, gradeID, statusID);
    }

    @Override
    public ResponseResult getAlarmList(String alarmId, String startTime, String endTime) {
        if (TextUtils.isEmpty(alarmId)) {
            return ResponseResult.success(mapper.getAlarmList());
        }
        return ResponseResult.success(mapper.getAlarmById(alarmId));
    }

    @Override
    public ResponseResult getRainInfo(String stcd, Date bt, Date et) {
        return ResponseResult.success(mapper.getRainInfo(stcd, bt, et));
    }

    @Override
    public ResponseResult getRsvrInfo(String stcd, Date bt, Date et) {
        return ResponseResult.success(mapper.getRsvrInfo(stcd, bt, et));
    }

    @Override
    public ResponseResult getRiverById(String stcd, Date bt, Date et) {
        return ResponseResult.success(mapper.getRiverInfo(stcd, bt, et));
    }

    @Override
    public int getWarnById(String warnId, String statusID) {
        Map<String, Integer> result = mapper.getWarnById(warnId, statusID);
        return result == null ? 0 : result.get("msgcount");
    }
}
